package com.itxiuyixiu.volunteer.controller;

import com.itxiuyixiu.entity.model.Organization;
import com.itxiuyixiu.tools.util.returns.ReturnVo;
import com.itxiuyixiu.volunteer.service.QiniuyunService;
import com.itxiuyixiu.volunteer.service.VerificationService;
import com.itxiuyixiu.volunteer.vo.QiniuyunVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;

/**
 * @author 黄磊
 * @date 2020/5/7
 */
@Api(tags = "公共API")
@Validated
@RestController
@RequestMapping("/public")
public class PublicController {

    @Autowired
    private VerificationService verificationService;
    @Autowired
    private QiniuyunService qiniuyunService;

    @ApiOperation(value = "发送邮件" , response = String.class)
    @PostMapping("/email")
    public ReturnVo sendVerification(@NotEmpty(message = "email不能为空") @Email(message = "email必须为邮件格式") String email) {
        if (!verificationService.sendVerification(email)) {
            return ReturnVo.getFailureReturn("邮件由于未知原因发送失败，请稍后重新发送");
        } else {
            return ReturnVo.getSuccessReturn("邮件发送成功，请注意查收");
        }
    }

    @ApiOperation(value = "上传七牛云照片" ,response = QiniuyunVo.class)
    @PostMapping("/photo")
    public ReturnVo getUploadPhotoToken() {
        return ReturnVo.getSuccessReturn(qiniuyunService.getUploadPhotoToken());
    }

    @ApiOperation(value = "删除七牛云照片")
    @DeleteMapping("/photo")
    public ReturnVo deleteFile(@RequestParam String key) {
        switch (qiniuyunService.deleteFile(key)) {
            case 0: return ReturnVo.getSuccessReturn("成功删除文件");
            case 1: return ReturnVo.getFailureReturn("删除的文件不属于您");
            case 2: return ReturnVo.getFailureReturn("删除文件失败，可能文件已经被删除");
            default:return ReturnVo.getFailureReturn("系统发生未知错误");
        }
    }
}
